package org.dark.fund.service;

import org.dark.fund.api.TokenCacheService;
import org.dark.fund.api.model.AccessToken;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@CacheConfig(cacheNames = "token")
public class TokenCacheServiceImpl implements TokenCacheService<AccessToken> {

    private final static Map<String, AccessToken> cache = new ConcurrentHashMap<>();

    @Cacheable(key = "#key", condition = "#result != null ")
    @Override
    public AccessToken get(String key) {
        return cache.get(key);
    }

    @CachePut(key = "#key")
    @Override
    public AccessToken put(String key, AccessToken value) {
        return cache.put(key, value);
    }

    @CacheEvict(key = "#key")
    @Override
    public void evict(String key) {
        cache.remove(key);
    }
}
